# Copyright (c) 2008-2009 Mikhail Chernov 
#
# This file is part of dcExplorer.
#
# dcExplorer is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# dcExplorer is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with dcExplorer.  If not, see <http://www.gnu.org/licenses/>.

package dcExplorer::layer::Fleet;

sub new
{
	my $proto = shift;
	my $class = ref($proto) || $proto;
	my $self = {};
	$self->{View} = shift;
	$self->{Empire} = shift;
	bless ($self, $class);
	return $self;
}

sub showFleet
{
	my $self = shift;
	my $fleet = shift;
	my $empire = $self->{Empire};
	my $canvas = $self->{Canvas};
	my ($x, $y) = map {$fleet->getProp($_) < $canvas->{'min_'.$_} ? $fleet->getProp($_) + 2000 : $fleet->getProp($_)} qw(x y);
	my $tta = $fleet->getProp('turns_till_arrival') || 0;
	my ($from_x, $from_y) = map {$fleet->getProp('from_'.$_) < $canvas->{'min_'.$_} ? $fleet->getProp($_) + 2000 : $fleet->getProp($_)} qw(x y) 
		if $tta > 0;
	return if (($x > $canvas->{max_x} || $y > $canvas->{max_y}) && ($tta == 0 || $from_x > $canvas->{max_x} || $from_y > $canvas->{max_y})); 

	my ($fg, $penSize) = $self->{View}->transformControlled($fleet, $empire);

	if (defined($fleet->getProp('turns_till_arrival')) && $fleet->getProp('turns_till_arrival') > 0)
	{
		$canvas->Line($canvas->center(map {$fleet->getProp($_)} qw(from_x from_y)), $canvas->center($x, $y), $fg);
	}
	
	my @rect = $canvas->rect($x, $y);
	$rect[0] += int(($penSize + 1) / 2);
	$rect[1] += int(($penSize + 1) / 2);
	$rect[2] -= int($penSize / 2);
	$rect[3] -= int($penSize / 2);
	$canvas->Rectangle(@rect, $fg, $penSize + 1);
}

sub showEnemyFleet
{
	my $self = shift;
	my $fleet = shift;
	my $empire = $self->{Empire};
	my $canvas = $self->{Canvas};
	my ($x, $y) = map {$fleet->getProp($_) < $canvas->{'min_'.$_} ? $fleet->getProp($_) + 2000 : $fleet->getProp($_)} qw(x y);
	return if ($x > $canvas->{max_x} || $y > $canvas->{max_y}); 
	my ($fg, $penSize) = $self->{View}->transformEnemy($fleet, $empire);

	my @rect = $canvas->rect($x, $y);
	$rect[0] += $penSize;
	$rect[1] += $penSize;
	$rect[2] -= $penSize;
	$rect[3] -= $penSize;
	$canvas->Line($rect[0], $rect[1], $rect[2], $rect[3], $fg, 1 + 2 * $penSize);
	$canvas->Line($rect[0], $rect[3], $rect[2], $rect[1], $fg, 1 + 2 * $penSize);
}

sub showControlledFleets
{
	my $self = shift;
	my $empire = $self->{Empire};
	$self->showFleet($_) foreach ($empire->fleets->getFleetsByName('%'), $empire->shared_fleets->getFleetsByName('%'));
}

sub showSharedFleets
{
	my $self = shift;
	my $empire = $self->{Empire};
	$self->showFleet($_) foreach grep {$_->getProp('shared')} $empire->alien_fleets->getFleetsByName('%');
}

sub showEnemyFleets
{
	my $self = shift;
	my $empire = $self->{Empire};
	$self->showEnemyFleet($_) foreach (
		grep 
			!defined($_->getProp('relation_nature')) || $_->getProp('relation_nature') < 1, 
			$empire->alien_fleets->getFleetsByName('%')
	)
}

sub setEmpire
{
	my $self = shift;
	$self->{Empire} = shift;
}

sub display
{
	my $self = shift;
	$self->{Canvas} = shift;
	$self->showEnemyFleets();
	$self->showSharedFleets();
	$self->showControlledFleets();
}

1;

